﻿namespace KinonekoSoftware.Studio.Galleries
{
    partial class InstanceGalleryViewModel
    {
        private InstanceGalleryMode _mode;

        private bool _isSelectionMode;
        private bool _isEditorMode;
        private bool _isGroupingMode;
        private bool _isStoryEditMode;
        private bool _isInspirationEditMode;
        private bool _isSettingEditMode;
        
        private bool _isDescending;
        private bool _isSortBySettingPanelOpen;
        private bool _isSearchByPanelOpen;

        public bool IsSearchByPanelOpen
        {
            get => _isSearchByPanelOpen;
            set => SetValue(ref _isSearchByPanelOpen, value);
        }

        public bool IsSortByPanelOpen
        {
            get => _isSortBySettingPanelOpen;
            set => SetValue(ref _isSortBySettingPanelOpen, value);
        }

        private void OnModeChanged()
        {


            //--+----------------+------------------+-----------------+
            //  |                |                  |                 |
            //  |  Mode ( 4bit)  |  SideBar (4bit)  | Partial (6bit)  |
            //  |                |                  |                 |
            //--+----------------+------------------+-----------------+
            //
            //
            // Mode:
            //
            // 1. SelectionMode = 0b0010
            // 2. EditorMode    = 0b0100
            //
            // SideBar:
            //
            // 1. None                  0bxxx_0001_xxx
            // 2. Preview               0bxxx_0010_xxx
            // 3. Categorized           0bxxx_0100_xxx
            //
            // Partial:
            //
            // 1. General               0bxxx_xxx_000001
            // 2. Group                 0bxxx_xxx_000010
            // 3. SimplifyEditor        0bxxx_xxx_000100
            // 4. StoryEditor           0bxxx_xxx_001000
            // 5. Inspiration           0bxxx_xxx_010000
            var mode = (int)_mode;

            IsSelectionMode = (mode & 0b0010_0000_000000) == 0b0010_0000_000000;
            IsEditorMode    = (mode & 0b0100_0000_000000) == 0b0100_0000_000000;
            
            IsGroupingMode        = (mode & 0b0000_0000_000010) == 0b0000_0000_000010;
            IsSettingEditMode     = (mode & 0b0000_0000_000100) == 0b0000_0000_000100;
            IsStoryEditMode       = (mode & 0b0000_0000_001000) == 0b0000_0000_001000;
            IsInspirationEditMode = (mode & 0b0000_0000_010000) == 0b0000_0000_010000;
        }

        private void OnSortByChanged()
        {
            // Z -> A
            var v = (int)_sortBy;
            IsDescending = (v & 0b0000_01) == 0b0000_01;
        }


        /// <summary>
        /// 获取或设置 <see cref="IsDescending"/> 属性。
        /// </summary>
        public bool IsDescending
        {
            get => _isDescending;
            private set => SetValue(ref _isDescending, value);
        }

        /// <summary>
        /// 获取或设置 <see cref="Mode"/> 属性。
        /// </summary>
        public InstanceGalleryMode Mode
        {
            get => _mode;
            set
            {
                SetValue(ref _mode, value);
                OnModeChanged();
            }
        }

        public bool HasAnyBinding
        {
            get
            {
                if (Selected is null)
                {
                    return false;
                }

                var state = !string.IsNullOrEmpty(Selected.InstanceID) ||
                            !string.IsNullOrEmpty(Selected.StyleID)    ||
                            !string.IsNullOrEmpty(Selected.WikiID);
                return state;
            }
        }

        public bool IsGroupingMode
        {
            get => _isGroupingMode;
            private set => SetValue(ref _isGroupingMode, value);
        }


        /// <summary>
        /// 获取或设置 <see cref="IsSettingEditMode"/> 属性。
        /// </summary>
        public bool IsSettingEditMode
        {
            get => _isSettingEditMode;
            set => SetValue(ref _isSettingEditMode, value);
        }
        /// <summary>
        /// 获取或设置 <see cref="IsInspirationEditMode"/> 属性。
        /// </summary>
        public bool IsInspirationEditMode
        {
            get => _isInspirationEditMode;
            set => SetValue(ref _isInspirationEditMode, value);
        }

        /// <summary>
        /// 获取或设置 <see cref="IsStoryEditMode"/> 属性。
        /// </summary>
        public bool IsStoryEditMode
        {
            get => _isStoryEditMode;
            private set => SetValue(ref _isStoryEditMode, value);
        }

        public bool IsEditorMode
        {
            get => _isEditorMode;
            private set => SetValue(ref _isEditorMode, value);
        }

        public bool IsSelectionMode
        {
            get => _isSelectionMode;
            private set => SetValue(ref _isSelectionMode, value);
        }
    }
}